<template>
  <header class="header">
    <!-- 保留第一个人的Banner区域 -->
    <div class="Bannerboxs" v-if="showBanner">
      <img class="banners" v-if="$route.path === '/windowmembrane'" src="@/assets/images/banner/qichemo.jpg" />
      <img class="banners" v-else-if="$route.path === '/paintprotection'" src="@/assets/images/banner/qimian.jpg" />
      <img class="banners" v-else-if="$route.path === '/construction'" src="@/assets/images/banner/jiaju.jpg" />
      <img class="banners" v-else-if="$route.path === '/aboutus'" src="@/assets/images/banner/about.jpg" />
      <img class="banners" v-else-if="$route.path.startsWith('/news')" src="@/assets/images/banner/news.jpg" />
      <img class="banners" v-else-if="$route.path === '/store'" src="@/assets/images/banner/store.jpg" />
    </div>

    <!-- 使用第二个人的滑块逻辑 -->
    <nav class="mainNav" :class="{ scrolled: isScrolled }">
      <ul>
        <!-- 产品中心 -->
        <li @mouseenter="handleMouseEnter(0)" @mouseleave="handleMouseLeave">
          <a>产品中心</a>
          <div class="subNav" ref="subNavProduct">
            <div class="list">
              <div class="item"><router-link to="/windowmembrane">汽车窗膜</router-link></div>
              <div class="item"><router-link to="/paintprotection">漆面保护膜</router-link></div>
              <div class="item"><router-link to="/construction">建筑家居保护膜</router-link></div>
            </div>
          </div>
        </li>

        <!-- 服务中心 -->
        <li @mouseenter="handleMouseEnter(1)" @mouseleave="handleMouseLeave">
          <a>服务中心</a>
          <div class="subNav" ref="subNavService">
            <div class="list">
              <div class="item"><router-link to="/store">门店查询</router-link></div>
              <div class="item"><a @click="zhibao">质保查询</a></div>
            </div>
          </div>
        </li>

        <!-- Logo -->
        <li ref="logoRef" @mouseenter="handleMouseEnter(2)" @mouseleave="handleMouseLeave">
          <router-link to="/">
            <img class="logos" src="/static/logo.png" alt="Logo" />
          </router-link>
        </li>

        <!-- 关于我们 -->
        <li @mouseenter="handleMouseEnter(3)" @mouseleave="handleMouseLeave">
          <a>关于我们</a>
          <div class="subNav" ref="subNavAbout">
            <div class="list">
              <div class="item"><router-link to="/aboutus">关于我们</router-link></div>
              <div class="item"><router-link to="/news">新闻资讯</router-link></div>
            </div>
          </div>
        </li>

        <!-- English -->
        <li class="last">
          <a @click="goother">English</a>
        </li>

        <!-- 滑块元素 -->
        <li class="slider" ref="sliderRef"></li>
      </ul>
    </nav>
  </header>
</template>

<script setup>
import { ref, onMounted, watch, computed } from 'vue'
import { useRoute } from 'vue-router'

const route = useRoute()
const sliderRef = ref(null)
const logoRef = ref(null)
const isScrolled = ref(false)

// 下拉菜单引用
const subNavProduct = ref(null)
const subNavService = ref(null)
const subNavAbout = ref(null)

// 默认位置索引（Logo是第三个，索引为2）
const defaultPositionIndex = 2
let currentPositionIndex = defaultPositionIndex
const navItemWidth = 300 // 与第一个人的宽度一致
const sliderWidth = 332 // 第二个人的滑块宽度

// 显示Banner的条件
const showBanner = computed(() => {
  const paths = ['/windowmembrane', '/paintprotection', '/construction', '/aboutus', '/store']
  return paths.includes(route.path) || route.path.startsWith('/news')
})

// 初始化滑块位置
onMounted(() => {
  moveSlider(defaultPositionIndex)
  window.addEventListener('scroll', handleScroll)
})

// 滚动处理（第一个人的逻辑）
const handleScroll = () => {
  isScrolled.value = window.scrollY > window.innerHeight
}

// 滑块动画（第二个人的逻辑）
const handleMouseEnter = (index) => {
  moveSlider(index)
}

const handleMouseLeave = () => {
  moveSlider(defaultPositionIndex)
}

const moveSlider = (index) => {
  if (!sliderRef.value) return

  currentPositionIndex = index
  const leftPosition = index * navItemWidth + (navItemWidth - sliderWidth) / 2
  sliderRef.value.style.left = `${leftPosition}px`

  // 控制子菜单显示
  toggleSubNav(0, index === 0)
  toggleSubNav(1, index === 1)
  toggleSubNav(3, index === 3)
}

// 子菜单显示控制
const toggleSubNav = (type, show) => {
  const subNav = type === 0 ? subNavProduct.value :
    type === 1 ? subNavService.value :
      subNavAbout.value
  if (!subNav) return

  if (show) {
    subNav.style.opacity = '1'
    subNav.style.visibility = 'visible'
    subNav.style.transform = 'translateX(-50%) translateY(-0px)'
  } else {
    subNav.style.opacity = '0'
    subNav.style.visibility = 'hidden'
    subNav.style.transform = 'translateX(-50%) translateY(-15px)'
  }
}

// 第一个人的路由方法
const zhibao = () => {
  window.open("https://qs.kdxwindowfilm.com/qualitySearch")
}

const goother = () => {
  // English跳转逻辑
  window.open("https://www.kdxfilms.com")
}
</script>

<style scoped lang="less">
/* 第一个人的样式基础 */
.bannerboxs {
  position: relative;
}

.Bannerboxs {
  height: 100vh;
  overflow: hidden;
}

.banners {
  width: 100%;
  display: block;
  transition: all 0.5s;
}

.banners:hover {
  transform: scale(1.1);
}

/* 第二个人的导航样式 */
.mainNav {
  position: absolute;
  width: 100%;
  top: 0;
  left: 0;
  z-index: 9999;
  background-color: transparent;
  transition: background-color 0.5s ease;
  display: flex;
  justify-content: center;
}

.mainNav.scrolled {
  background-color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.mainNav ul {
  display: flex;
  justify-content: center;
  position: relative;
  list-style: none;
  padding: 0;
  margin: 0;
}

.mainNav li {
  width: 300px;
  /* 第一个人的宽度 */
  height: 120px;
  font-size: 28px;
  /* 第一个人的字体大小 */
  position: relative;
  text-align: center;

  &.slider {
    width: 332px;
    height: 139px;
    position: absolute !important;
    background: url("/static/boli.png");
    background-size: 100% 100%;
    left: 0;
    top: 0;
    transition: left 0.3s ease;
    z-index: -1;
    /* 在导航项下方 */
    pointer-events: none;
  }
}

.mainNav a {
  color: white;
  display: block;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  text-decoration: none;
  cursor: pointer;
}

.mainNav.scrolled a {
  color: #333;
}

/* Logo样式 */
.logos {
  width: 125px;
  display: block;
}

/* 滑块样式（第二个人） */


/* 子菜单样式（融合两人特点） */
.subNav {
  width: 264px;
  padding: 20px 0;
  position: absolute;
  left: 50%;
  top: 120px;
  border-radius: 8px;
  opacity: 0;
  visibility: hidden;
  transform: translateX(-50%) translateY(-10px);
  transition: all 0.3s ease;
  z-index: 10;
  background: url("/static/subNav.png") center bottom;
  background-size: 100% auto;
  backdrop-filter: blur(6px);
}

.subNav .item {
  padding: 18px 40px;
}

.subNav .item a {
  color: #ccc;
  font-size: 18px;
  justify-content: flex-start;
  height: auto;
}

.subNav .item a:hover {
  color: white;
}
</style>